//
//  CALayer+ZHExtension.m
//  KLCategory
//
//  Created by zhusanbao on 2020/10/5.
//

#import "CALayer+ZHExtension.h"

@implementation CALayer (ZHExtension)

- (void)transitionWithType:(CATransitionType)type subtype:(CATransitionSubtype)subtype{
    [self transitionWithType:type subtype:subtype duration:0.35f];
}

- (void)transitionWithType:(CATransitionType)type subtype:(CATransitionSubtype)subtype duration:(NSTimeInterval)duration{
    [self transitionWithType:type subtype:subtype duration:duration function:kCAMediaTimingFunctionEaseOut];
}

- (void)transitionWithType:(CATransitionType)type subtype:(CATransitionSubtype)subtype duration:(NSTimeInterval)duration function:(CAMediaTimingFunctionName)function{
    //创建CATransition对象
    CATransition *animation = [CATransition animation];
    //设置运动时间
    animation.duration = duration;
    //设置运动type
    animation.type = type;
    if (subtype != nil) {
        //设置子类
        animation.subtype = subtype;
    }
    //设置运动速度
    animation.timingFunction = [CAMediaTimingFunction functionWithName:function ?: kCAMediaTimingFunctionEaseIn];
    [self addAnimation:animation forKey:@"kl.transition"];
}

- (void)fadeAnimationWithDuration:(NSTimeInterval)duration curve:(UIViewAnimationCurve)curve {
    if (duration <= 0) return;
    NSString *mediaFunction;
    switch (curve) {
        case UIViewAnimationCurveEaseInOut: {
            mediaFunction = kCAMediaTimingFunctionEaseInEaseOut;
        } break;
        case UIViewAnimationCurveEaseIn: {
            mediaFunction = kCAMediaTimingFunctionEaseIn;
        } break;
        case UIViewAnimationCurveEaseOut: {
            mediaFunction = kCAMediaTimingFunctionEaseOut;
        } break;
        case UIViewAnimationCurveLinear: {
            mediaFunction = kCAMediaTimingFunctionLinear;
        } break;
        default: {
            mediaFunction = kCAMediaTimingFunctionLinear;
        } break;
    }
    CATransition *transition = [CATransition animation];
    transition.duration = duration;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:mediaFunction];
    transition.type = kCATransitionFade;
    [self addAnimation:transition forKey:@"kl.fade"];
}

- (void)removeLastFadeAnimation {
    [self removeAnimationForKey:@"kl.fade"];
}

- (void)poperPushSpringWithKeyPath:(NSString *)keyPath handle:(void (^)(CASpringAnimation * _Nonnull))handle{
    if (keyPath) {
        CASpringAnimation *animation = [CASpringAnimation animationWithKeyPath:keyPath];
        animation.additive          = YES;
        animation.initialVelocity   = 0;
        animation.damping           = 500;
        animation.stiffness         = 1000;
        animation.mass              = 3;
        animation.fillMode          = kCAFillModeBoth;
        animation.timingFunction    = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        animation.removedOnCompletion = NO;
        if (handle) handle(animation);
        animation.duration          = animation.settlingDuration;
        [self addAnimation:animation forKey:@"poper.spring"];
    }
}

@end
